package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class Activity_Handler extends Activity {

    private Button button = null;

    private int progressNum;
    private ProgressBar progressBar = null;
    private Handler handler = null;
    private Message msg = null;
    private int frequency;

    /**
     * 初始化
     */
    private void initWidget() {

        this.progressBar = (ProgressBar) findViewById(R.id.horizontalProgress);
        this.button = (Button) findViewById(R.id.button);
        this.frequency = progressBar.getMax() / 10;

        this.handler = new Handler() {

            /**
             * handlerMessage(Message msg)是导步的，系统自动调用
             * @param msg
             */
            @Override
            public void handleMessage(Message msg) {

                /*
                 * 设置进度条
                 */
                progressBar.setProgress(msg.arg1);
                progressBar.setSecondaryProgress(msg.arg1 + 10);

                if(msg.arg1 >= progressBar.getMax()){
                    progressBar.setVisibility(View.GONE);
                    progressNum = 0;
                    progressBar.setProgress(progressNum);
                    progressBar.setSecondaryProgress(progressNum);
                    return;
                }
                /**
                 * 再次压入线程队列中
                 */
                post(handThread);
            }
        };
    }



    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.initWidget();
        this.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                handler.post(handThread);
            }
        });
    }

    /**
     * 被handler处理的线程
     */
    Runnable handThread = new Runnable() {
        @Override
        public void run() {

            System.out.println("Begin thread...");

            progressNum += frequency;

            msg = handler.obtainMessage();
            msg.arg1 = progressNum;

            /**
             * 向消息队列压入消息
             */
            handler.sendMessage(msg);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            if (progressNum >= progressBar.getMax()) {
                handler.removeCallbacks(this);
                System.out.println("progressNum is: " + progressNum);
            }
        }
    };
}
